﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DelonixRegiaHMSys.Core.Services;
using DelonixRegiaHMSys.Infrastructure.Data;
using DelonixRegiaHMSys.Core.Models;

namespace DelonixRegiaHMSys.UI.WebForms
{
    public partial class CheckOutManagement : System.Web.UI.Page
    {
        string userID;
        string cid;
        decimal b;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string ID = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                string strid = "";
                Random rnd = new Random();
                for (int i = 0; i <= 3; i++)
                {
                    int randomint = rnd.Next(0, ID.Length - 1);
                    strid += ID.Substring(randomint, 2);
                }
                lblCheckOutID.Text = strid;
                strid = "";
                for (int i = 0; i <= 3; i++)
                {
                    int randomint = rnd.Next(0, ID.Length - 1);
                    strid += ID.Substring(randomint, 2);
                }
                lblInvoiceID.Text = strid;
            }
            userID = Request.QueryString["UserID"];
            cid = Request.QueryString["CheckInID"];
            lblRoomNum.Text = Request.QueryString["RoomNum"];
            lblRoomRates.Text = Request.QueryString["RoomRates"];
            lblGuestID.Text = Request.QueryString["GuestID"];
            lblGuestFN.Text = Request.QueryString["GuestFN"];
            lblGuestLN.Text = Request.QueryString["GuestLN"];
            var ciservice = new ciServices(new EfCheckInQuery());
            lblCheckInDateTime.Text = Convert.ToString(ciservice.GetCheckinDateTime(cid));
            lblCheckOutDateTime.Text = Convert.ToString(DateTime.Now);
            TimeSpan s = Convert.ToDateTime(lblCheckOutDateTime.Text) - Convert.ToDateTime(lblCheckInDateTime.Text);
            lblnumOfNightsStayed.Text = Convert.ToString(Math.Round(s.TotalDays - 1));
            lblPM.Text = Request.QueryString["PaymentMode"];
        }

        private void BindGridView()
        {

            GridViewItems.DataSource = Session["items"];
            GridViewItems.DataBind();
        }

        protected void additembtn_Click(object sender, EventArgs e)
        {
            Item item = new Item();
            item.ItemName = itmNametbx.Text;
            item.Description = desctbx.Text;
            item.Cost = Convert.ToDecimal(costtbx.Text);
            if (Session["items"] == null)
            {
                Session["items"] = new List<Item>();
            }
            var items = (List<Item>)Session["items"];
            items.Add(item);
            itmNametbx.Text = string.Empty;
            desctbx.Text = string.Empty;
            costtbx.Text = string.Empty;
            BindGridView();
        }

        protected void Invoicebtn_Click(object sender, EventArgs e)
        {

            var coservice = new coServices(new EfCheckOutQuery());
            Invoice inv = new Invoice();
            inv.InvoiceID = lblInvoiceID.Text;
            inv.GuestID = lblGuestID.Text;
            inv.UserID = userID;
            inv.CheckinID = cid;
            inv.CheckOutDateTime = Convert.ToDateTime(lblCheckOutDateTime.Text);
            inv.RoomNum = Convert.ToInt32(lblRoomNum.Text);
            inv.NumberOfNightsStayed = Convert.ToInt32(lblnumOfNightsStayed.Text);

            var items = (List<Item>)Session["items"];
            foreach (Item a in items)
            {
                b = b + a.Cost;
            }

            inv.AddCosts = b;
            decimal h = (7 / 100) * b;
            inv.TotalCosts = b + h;
            coservice.InsertInvoice(inv);

            foreach (Item c in items)
            {
                Item a = new Item();
                a.ItemName = c.ItemName;
                a.Description = c.Description;
                a.Cost = c.Cost;
                a.InvoiceID = inv.InvoiceID;
                coservice.InsertItem(a);
            }

            CheckOut co = new CheckOut();
            co.CheckOutID = lblCheckOutID.Text;
            co.CheckInID = cid;
            co.UserID = userID;
            co.InvoiceID = inv.InvoiceID;

            var roservice = new roServices(new EfRoomQuery());
            roservice.UpdateRoom(Convert.ToInt32(lblRoomNum.Text), "SFC", null);

            string queryString = "UserID=" + userID + "&GuestID=" + lblGuestID.Text + "&InvoiceNum=" + lblInvoiceID.Text;
            Server.Transfer("~/WebForms/GenerateInvoice.aspx?" + queryString);
        }

    }
}